package cn.iocoder.yudao.module.question.controller.admin.question;

import cn.iocoder.yudao.module.question.controller.admin.questionitem.vo.QuestionItemExportReqVO;
import cn.iocoder.yudao.module.question.convert.questionitem.QuestionItemConvert;
import cn.iocoder.yudao.module.question.dal.dataobject.questionitem.QuestionItemDO;
import cn.iocoder.yudao.module.question.service.questionitem.QuestionItemService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.question.controller.admin.question.vo.*;
import cn.iocoder.yudao.module.question.dal.dataobject.question.QuestionDO;
import cn.iocoder.yudao.module.question.convert.question.QuestionConvert;
import cn.iocoder.yudao.module.question.service.question.QuestionService;

@Tag(name = "管理后台 - 试题信息")
@RestController
@RequestMapping("/question/question")
@Validated
public class QuestionController {

    @Resource
    private QuestionService Service;

    @Resource
    private QuestionItemService questionItemService;

    @PostMapping("/create")
    @Operation(summary = "创建试题信息")
    @PreAuthorize("@ss.hasPermission('question::create')")
    public CommonResult<Long> create(@Valid @RequestBody QuestionCreateReqVO createReqVO) {
        return success(Service.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试题信息")
    @PreAuthorize("@ss.hasPermission('question::update')")
    public CommonResult<Boolean> update(@Valid @RequestBody QuestionUpdateReqVO updateReqVO) {
        Service.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试题信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('question::delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        Service.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试题信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('question::query')")
    public CommonResult<QuestionRespVO> get(@RequestParam("id") Long id) {
        QuestionDO question  = Service.get(id);
        QuestionRespVO questionRespVO = QuestionConvert.INSTANCE.convert(question);
        List<QuestionItemDO> questionItemDOList = questionItemService.getItemList(QuestionItemExportReqVO.builder()
                .questionId(id)
                .build());
        questionRespVO.setQuestionItemList(QuestionItemConvert.INSTANCE.convertList(questionItemDOList));
        return success(questionRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得试题信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('question::query')")
    public CommonResult<List<QuestionRespVO>> getList(@RequestParam("ids") Collection<Long> ids) {
        List<QuestionDO> list = Service.getList(ids);
        return success(QuestionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得试题信息分页")
    @PreAuthorize("@ss.hasPermission('question::query')")
    public CommonResult<PageResult<QuestionRespVO>> getPage(@Valid QuestionPageReqVO pageVO) {
        PageResult<QuestionDO> pageResult = Service.getPage(pageVO);
        return success(QuestionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试题信息 Excel")
    @PreAuthorize("@ss.hasPermission('question::export')")
    @OperateLog(type = EXPORT)
    public void exportExcel(@Valid QuestionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<QuestionDO> list = Service.getList(exportReqVO);
        // 导出 Excel
        List<QuestionExcelVO> datas = QuestionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "试题信息.xls", "数据", QuestionExcelVO.class, datas);
    }

}
